<?php
	/**
	 +=========================================================
	 * 零度空间 ZD cms 网站管理系统
	 * SystemAction.class.php 系统设置类
	 +=========================================================
	 * @copyright © 2011 nickdraw.com All rights reserved.
	 * @author NickDraw(零度温柔) webmaster@206c.net
	 * @license http://www.nickdraw.com/license
	 +=========================================================
	 */
	
	class SystemAction extends CommonAction{
		public function index(){
			$map['conf_group'] = 'all';
			$map['conf_var'] = 'settings';
			$map['conf_type'] = array('neq','hidden');
			$this->_vlist($map);
		}
		
		public function update(){
			$model = D('system');
			
			foreach($_POST['conf_list'] as $key=>$val){
				if ($key == 21 || $key == 22) $val = stripslashes($val);
				$model->where("id='".$key."'")->setField('conf_value',trim($val));
			}
			
			if ($_POST['common'] || $_POST['attachment']){
				$map['conf_group'] = 'all';
				$list = $model->where($map)->select();
				$this->confCreate($list);
			}
			
			if($_POST['system']){
				$map['conf_group'] = 'admin';
				$list = $model->where($map)->select();

				import("ORG.Util.EditFile");
				$nfile = new EditFile();
				$nfile->fName = './configs/config.inc.php';
				
				$write = '<?php'."\r\n";
				$write .= '	/**'."\r\n";
				$write .= '	 +========================================================='."\r\n";
				$write .= '	 * 零度空间 ZD cms 网站管理系统'."\r\n";
				$write .= '	 * config.inc.php 网站主配置'."\r\n";
				$write .= '	 +========================================================='."\r\n";
				$write .= '	 * @copyright © 2012 nickdraw.com All rights reserved.'."\r\n";
				$write .= '	 * @author NickDraw(零度温柔) webmaster@206c.net'."\r\n";
				$write .= '	 * @license http://www.nickdraw.com/license'."\r\n";
				$write .= '	 +========================================================='."\r\n";
				$write .= '	 */'."\r\n";
				$write .= '    $array  = array('."\r\n";
				foreach($list AS $key=>$val){
					if ($val['conf_value'] == 'true' || $val['conf_value'] == 'false' || is_numeric($val['conf_value']) == true){
						$write .= '        "'.$val['conf_key'].'" => '.$val['conf_value'].', //'.$val['conf_name']."\r\n";
					}else{
						$write .= '        "'.$val['conf_key'].'" => "'.$val['conf_value'].'", //'.$val['conf_name']."\r\n";
					}
				}
				
				$write .= '    );'."\r\n";
				$write .= '?>'."\r\n";
				
				$nfile->WriteToNull($write);
			}
			
			$this->success("修改成功");
			
		}
		
		public function system(){
			$map['conf_group'] = 'admin';
			$map['conf_var'] = 'system';
			$this->_vlist($map);
		}
		
		public function attachment(){
			$map['conf_group'] = 'all';
			$map['conf_var'] = 'attachment';
			$this->_vlist($map);
		}
        
        public function modtype(){
            $model = M('category_type');
            $list = $model->select();
            $this->assign("list",$list);
            
            $this->display();
        }
		
		private function _vlist($map){
			$model = M('system');
			
			$list = $model->where( $map )->select();
			
			$this->assign("list",$list);
			$this->display();
		}
		
		private function confCreate($data){
			import("ORG.Util.EditFile");
			$nfile = new EditFile();
			$nfile->fName = './configs/settings.inc.php';
			
			$write_item = '<?php'."\r\n";
			$write_item .= '	/**'."\r\n";
			$write_item .= '	 +========================================================='."\r\n";
			$write_item .= '	 * 零度空间 ZD cms 网站管理系统'."\r\n";
			$write_item .= '	 * settings.inc.php 网站配置参数'."\r\n";
			$write_item .= '	 +========================================================='."\r\n";
			$write_item .= '	 * @copyright © 2012 nickdraw.com All rights reserved.'."\r\n";
			$write_item .= '	 * @author NickDraw(零度温柔) webmaster@206c.net'."\r\n";
			$write_item .= '	 * @license http://www.nickdraw.com/license'."\r\n";
			$write_item .= '	 +========================================================='."\r\n";
			$write_item .= '	 */'."\r\n";
			$write_item .= '    return array('."\r\n";
			foreach($data AS $key=>$val){
				if ($val['conf_value'] == 'true' || $val['conf_value'] == 'false' || is_numeric($val['conf_value']) == true){
					$write_item .= '        "'.$val['conf_key'].'" => '.$val['conf_value'].', //'.$val['conf_name']."\r\n";
				}else{
					$write_item .= '        "'.$val['conf_key'].'" => "'.$val['conf_value'].'", //'.$val['conf_name']."\r\n";
				}
			}
			$write_item .= '    );'."\r\n";
			$write_item .= '?>'."\r\n";
			$nfile->WriteToNull($write_item);
		}
	}
	